const app = getApp()
var http = require('../../utils/server.js')
var util = require('../../utils/util.js')

Page({
  data: {
    pics: [],
    width: [],
    height: [],
    showTop: true,
    showDialog: false,
    indicatorDots: true,
    autoplay: false,
    interval: 3000,
    duration: 300,
    selectSpecification: {},
    data: {},
    priceMin: 0,
    priceMax: 0,
    product_id: '',
    shop_id: '',
    showAuthor: false,
    fansAwardIntro: false,
    hasFansReward: false,
    showGoIndex:false,
  },
  onLoad: function(option) {
    console.log('页面收到的参数', option)
    var userInfo = wx.getStorageSync('userInfo')
    this.setData({
      product_id: option.product_id || '',
      shop_id: option.shop_id || '',
      userInfo: userInfo,
      showGoIndex:option.from?true:false
    })
  },
  onShow: function() {
    this.getData()
  },
  onParentEvent: function(e) {
    app.loginServer(app.globalData.code, e.detail.encryptedData, e.detail.iv, this.getData)
  },
  getData(userInfo) {
    var that = this
    if (that.data.userInfo == null) {
      //用户信息
      app.getUserInfo(function(u) {
        if (u) {
          userInfo = u
        } else {
          that.setData({
            showAuthor: true
          })
        }
      })
    }
    that.setData({
      showAuthor: false
    })
    http.post({
      url: "product/getDetail",
      product_id: that.data.product_id,
      shop_id: that.data.shop_id
    }, function(data) {
      console.log('商品详情', data)
      var min = 99999999
      var max = 0
      var hasFansReward = false
      for (const item of data.specifications) {
        if (item.gang_price > max)
          max = item.gang_price
        if (min > item.gang_price)
          min = item.gang_price
        item.redPacket = (item.gang_price - item.supply_price - item.fans_reward).toFixed(2)
        item.redPacket = isNaN(item.redPacket) ? 0 : item.redPacket
        if (item.fans_reward > 0)
          hasFansReward = true
      }
      that.setData({
        data: data,
        priceMin: min,
        priceMax: max,
        hasFansReward: hasFansReward
      })
    })


  },
  imgHeight: function(e) {
    var index = e.currentTarget.dataset.index
    var imgw = e.detail.width;
    var imgh = e.detail.height;
    var width = this.data.width
    var height = this.data.height
    width[index] = imgw
    height[index] = imgh
    this.setData({
      width: width,
      height: height
    })
  },
  selectSpecificationTap(e) {
    var s = this.data.selectSpecification
    s.index = e.currentTarget.dataset.index
    s.data = this.data.data.specifications[s.index]
    this.setData({
      selectSpecification: s
    })
  },
  closeTop() {
    this.setData({
      showTop: !this.data.showTop
    })
  },
  toggleDialog(e) {
    var replace = e.currentTarget.dataset.replace || '1'
    console.log(replace)
    this.setData({
      showDialog: !this.data.showDialog,
      replaceOrder: replace
    })
  },
  transmitLink() {
    wx.navigateTo({
      url: '/pages/productDetails/forward/forward?product_id=' + this.data.product_id
    })
  },
  toOrdering() {
    if (!this.data.selectSpecification.data) {
      util.showError('请先选择规格')
      return
    }
    wx.navigateTo({
      url: '/pages/order/ordering/ordering?product_ids=' + this.data.product_id + "&specifications=" + this.data.selectSpecification.data.title + "&replaceOrder=" + this.data.replaceOrder + "&shop_id=" + this.data.shop_id,
    })
  },
  tip: function() {
    this.setData({
      fansAwardIntro: !this.data.fansAwardIntro
    })
  },
  showFansAwardDetail: function() {
    this.setData({
      fansAwardDetail: !this.data.fansAwardDetail
    })
  },
  showAfterSaleIntro: function() {
    this.setData({
      afterSaleIntro: !this.data.afterSaleIntro
    })
  },
  showPreviewImage: function(e) {
    var index = e.currentTarget.dataset.index
    wx.previewImage({
      current: this.data.data.pics[index],
      urls: this.data.data.pics
    })
  },
  goToIndex: function () {
    wx.reLaunch({
      url: '/pages/index/welcome'
    })
  },


})